package com.rongshu.view.discovery.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.discovery.bean.EventListResponse;
import com.rongshu.view.web.ActivityWeb;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * created by zhengweis on 2019/4/17.
 * description：
 */
public class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder> {

    private Context context;
    private List<EventListResponse.ResponseBean> list;

    public EventListAdapter(Context context, List<EventListResponse.ResponseBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_event_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        GlideUtils.loadImageView(context, viewHolder.eventImg, list.get(i).getImgUrl());
        viewHolder.eventTitle.setText(list.get(i).getTitle());
        viewHolder.eventTime.setText(list.get(i).getExplain());
        viewHolder.line.setVisibility(i == list.size() - 1 ? View.GONE : View.VISIBLE);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ActivityWeb.class);
                intent.putExtra(Constant.INTENT_TITLE, "活动");
                intent.putExtra(Constant.INTENT_H5, list.get(i).getUrlAddress());
                context.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.event_img)
        ImageView eventImg;
        @BindView(R.id.event_title)
        TextView eventTitle;
        @BindView(R.id.event_time)
        TextView eventTime;
        @BindView(R.id.event_state)
        TextView eventState;
        @BindView(R.id.line)
        View line;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
